/*只获取有来包的城市列表*/
function getCityHavePackage(){
    $(".city-sender option").remove();
	 $(".city-sender").html("<option></option>");
		
	    $.getJSON("../order_queryNewPackCity.json",function(data){
	        if(data.success && data.NewPackCity != null){
	            $.each(data.NewPackCity,function(index,item){
	            	 $(".city-sender").append("<option  value="+item+">"+item+"</option>");
	            });
	        }
	    });
}
/*只获取城市下确认收取的包裹号*/
function getPackageNoOfCity(packageCity){
	$(".package-number").html("<option></option>");
	$.getJSON("../packageManage_getPackage.json",{packageCity : packageCity},function(data){
        if(data.success && data.packages != null){
            $.each(data.packages,function(index,item){
            	 $(".package-number").append("<option  value="+item.packageNo+">"+item.packageNo+"</option>");
            });
        }
    });
}
/*获取订单*/
function getOrders(packageCity,packageNo){
	$(".td_data").remove();
	$.getJSON("../packageManage_getPackageOrders.json",{packageCity : packageCity,packageNo: packageNo},function(data){
        if(data.success && data.orders != null){
            $.each(data.orders,function(index,item){
                if(item.status==3){
                    $(".table-orders").append("<tr class='td_data'>" +
                        "<td>"+(item.isQuick?"加急件":"")+"</td>" +
                        "<td>"+item.orderNo+ "</td><td>"
                        +item.city+item.area+item.street+item.address+"</td><td>"
                        +item.user.stationName+"</td><td>"
                        +" <button  class='btn btn-primary btn-sm btn-table' onclick=\"isReceive("+"'receive','"+item.orderNo+"','收取成功')"+"\">收到</button>"+"&nbsp"+"<button class='btn btn-primary btn-sm btn-table' data-target='#errorMessage' data-toggle='modal' onclick=\"isReceive("+"'sendBack','"+item.orderNo+"','')"+"\">异常</button>"+"</td>"
                        );
                }
            });
            if($(".td_data").length==0){
				 $(".city-sender").change();
			}
        }
    });
}
/*收件,异常*/
function  isReceive(type,order,middleMessage){
	if(type=="sendBack"){
        $(".btn-save-error").unbind("click");
        $(".btn-save-error").click(function () {
            var errormsg =  $(".input-error-info").val();
            if(errormsg==""){
                window.wxc.xcConfirm("请输入反馈信息!", window.wxc.xcConfirm.typeEnum.warning);
            }else {
                middleMessage=errormsg;
                $.getJSON("../order_receivePackage.json",{type : type,order: order,middleMessage : middleMessage},function(data){
                    if(data.success){
                        // getCounterForPackage();
                        // getQuickerForPackage();
                        $(".input-error-info").val("");
                        $(".close").trigger("click");
                        $(".package-number").change();
                        window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
                    }
                });
            }
        })
    }
	else if(type=="receive"){
		$.getJSON("../order_receivePackage.json",{type : type,order: order,middleMessage : middleMessage},function(data){
			if(data.success){
                // getCounterForPackage();
                // getQuickerForPackage();
                $(".package_number").change();
				window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
			}
		});
	}
}
$(document).ready(function () {
    var packageCity;//发包城市
    var packageNo;//包裹号
    var receiveUser;//收包者
    var receiveDate;//收包时间
    var receiveMessage;//收包退回详情
    var deliverMessage;//派送失败原因
    getCityHavePackage();
    $(".city-sender").change(function(){
    	getPackageNoOfCity(this.value);
    })
    $(".package-number").change(function(){
    	var packageCity=$(".city-sender").val();
    	var packageNo=$(".package-number").val();
    	getOrders(packageCity,packageNo);
    	
    })
    
   
    
    
})